﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace host_manager
{
    public partial class Form1 : Form
    {
        string fpath = @"C:\WINDOWS\system32\drivers\etc\hosts";
        public Form1()
        {
            InitializeComponent();
    
            File.SetAttributes(@fpath, FileAttributes.Normal);
        }

        private void btn_open_Click(object sender, EventArgs e)
        {
            open_file();
        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            save_file();
        }
        void open_file()
        {
            text_box.Clear();
            text_box.Text = File.ReadAllText(fpath);
            status_label.Text = "完成..打开" + fpath;
        }
        void save_file()
        {
           
            File.WriteAllText(fpath, text_box.Text);
            text_box.Clear();
            open_file();
            status_label.Text = "完成..已保存" + fpath;
        }
        void restart_dns()
        {
            status_label.Text = Execute("ipconfig /flushdns", 10);
        }
        public static string Execute(string command, int seconds)
        {
            string output = ""; //输出字符串  
            if (command != null && !command.Equals(""))
            {
                Process process = new Process();//创建进程对象  
                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = "cmd.exe";//设定需要执行的命令  
                startInfo.Arguments = "/C " + command;//“/C”表示执行完命令后马上退出  
                startInfo.UseShellExecute = false;//不使用系统外壳程序启动  
                startInfo.RedirectStandardInput = false;//不重定向输入  
                startInfo.RedirectStandardOutput = true; //重定向输出  
                startInfo.CreateNoWindow = true;//不创建窗口  
                process.StartInfo = startInfo;
                try
                {
                    if (process.Start())//开始进程  
                    {
                        if (seconds == 0)
                        {
                            process.WaitForExit();//这里无限等待进程结束  
                        }
                        else
                        {
                            process.WaitForExit(seconds); //等待进程结束，等待时间为指定的毫秒  
                        }
                        output = process.StandardOutput.ReadToEnd();//读取进程的输出  
                    }
                }
                catch
                {
                }
                finally
                {
                    if (process != null)
                        process.Close();
                }
            }
            return output;
        }
        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if(e.KeyCode==Keys.O && e.Control)
            {
                e.Handled = true;
                open_file();
            }
            else if(e.KeyCode==Keys.S && e.Control)
            {
                e.Handled = true;
                save_file();
            }
        }

        private void btn_add_Click(object sender, EventArgs e)
        {
            if(text_ip.Text!=""&&text_domain.Text!="")
            {
                text_box.AppendText(Environment.NewLine);
                text_box.AppendText(text_ip.Text + "  " + text_domain.Text);
                status_label.Text = "增加一行到host";
            }
        }

        private void btn_refresh_Click(object sender, EventArgs e)
        {
            restart_dns();
        }
    }
}
